package com.example.controller;

import com.example.config.SMSProperties;
import com.example.dto.RegisterDto;
import com.example.dto.UserLoginDto;
import com.example.execption.ApiRestResponse;
import com.example.execption.TijianException;
import com.example.pojo.User;
import com.example.service.CodeService;
import com.example.service.impl.CodeServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/code")
@Slf4j
public class CodeController {

    @Autowired
    private SMSProperties smsProperties;

    @Autowired
    private CodeService codeService;

    private static final Logger logger = LoggerFactory.getLogger(CodeServiceImpl.class);

    /**
     * 发送验证码
     * @param request 请求体，包含 phone 字段
     * @return ResponseEntity<Void>
     */
    @PostMapping("/sendCode")
    public ResponseEntity<Void> getVICode(@RequestBody PhoneRequest request) {
        String phone = request.getPhone();
        try {
            codeService.getVICode(phone);
            return ResponseEntity.ok().build(); // 成功发送短信，返回 200 OK
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); // 发送短信失败，返回 500 错误
        }
    }

    // 内部类，用于接收 JSON 请求中的 phone 字段
    static class PhoneRequest {
        private String phone;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
    }
    //15818853756
    /**
     * 登陆验证码校验
     * @param userLoginDto
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<ApiRestResponse<User>> login(@RequestBody UserLoginDto userLoginDto) {
        try {
            // 增加日志输出，确保能够正确接收到参数
            log.info("Received login request: {}", userLoginDto);

            User user = codeService.login(userLoginDto);
            return ResponseEntity.ok(ApiRestResponse.success(user));
        } catch (Exception e) {
            log.error("Error processing login request", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(ApiRestResponse.error(500, "Internal Server Error"));
        }
    }
    /**
     * 用户验证码注册
     * @param registerDto
     * @return
     */
    @PostMapping("/register")
    public ApiRestResponse<User> register( RegisterDto registerDto) {
        log.info("Received registration request2222: {}", registerDto); // 增加日志输出
        return ApiRestResponse.success(codeService.register(registerDto));
    }
}
